package org.datascooter.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.datascooter.impl.DataSource;

public class DataSourceHandler {

	private static final String REGEX = "=";
	private static final String DATASOURCE = "<datasource>";
	private String filePath;
	private DataSource current;
	private List<DataSource> sourceList = new ArrayList<DataSource>();

	public DataSourceHandler(String filePath) throws IOException {
		this.filePath = filePath;
		load();
	}

	public void load() throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		try {
			String line;
			DataSource source = new DataSource();
			while ((line = reader.readLine()) != null) {
				if (line.equals("")) {
					continue;
				}
				if (line.equals(DATASOURCE)) {
					if (source.isCurrent()) {
						current = source;
					} else {
						sourceList.add(source);
					}
					source = new DataSource();
					continue;
				}
				String[] split = line.split(REGEX);
				if (split.length == 2) {
					if (split[0].equals("name")) {
						source.setName(split[1].trim());
					} else if (split[0].equals("url")) {
						source.setURL(split[1].trim());
					} else if (split[0].equals("user")) {
						source.setUser(split[1].trim());
					} else if (split[0].equals("password")) {
						source.setPassword(split[1].trim());
					} else if (split[0].equals("current")) {
						source.setCurrent(Boolean.valueOf(split[1].trim()));
					}
				}
			}
		} finally {
			reader.close();
		}
	}

	public void save() throws FileNotFoundException, IOException {
		StringBuilder builder = new StringBuilder();
		fillDataSource(builder, current);
		for (DataSource dataSource : sourceList) {
			fillDataSource(builder, dataSource);
		}
		new File(filePath).delete();
		new File(filePath).createNewFile();
		BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
		writer.write(builder.toString());
		writer.flush();
		writer.close();
	}

	private void fillDataSource(StringBuilder builder, DataSource dataSource) {
		builder.append("name=");
		builder.append(LangUtils.getDefault(dataSource.getName()));
		builder.append(SnipUtils.NL);
		builder.append("url=");
		builder.append(LangUtils.getDefault(dataSource.getURL()));
		builder.append(SnipUtils.NL);
		builder.append("user=");
		builder.append(LangUtils.getDefault(dataSource.getUser()));
		builder.append(SnipUtils.NL);
		builder.append("password=");
		builder.append(LangUtils.getDefault(dataSource.getPassword()));
		builder.append(SnipUtils.NL);
		builder.append("current=");
		builder.append(LangUtils.getDefault(dataSource.isCurrent()));
		builder.append(SnipUtils.NL);
		builder.append(DATASOURCE);
		builder.append(SnipUtils.NL);
		builder.append(SnipUtils.NL);
	}

	public DataSource getCurrent() {
		return current;
	}

	public List<DataSource> getSourceList() {
		return sourceList;
	}

	public List<String> getNameList() {
		List<String> names = new ArrayList<String>();
		if (current != null) {
			names.add(current.getName());
		}
		for (DataSource dataSource : sourceList) {
			names.add(dataSource.getName());
		}
		return names;
	}

	public DataSource getDataSource(String name) {
		for (DataSource dataSource : sourceList) {
			if (dataSource.getName().equals(name)) {
				return dataSource;
			}
		}
		if ((current != null) && current.getName().equals(name)) {
			return current;
		}
		return null;
	}

	public void setCurrent(DataSource current2) {
		if ((current != null) && (!current2.getName().equals(current.getName()))) {
			sourceList.remove(current);
			current.setCurrent(false);
			sourceList.add(current);
		}
		sourceList.remove(current2);
		current = current2;
		current.setCurrent(true);
	}

	public String getFilePath() {
		return filePath;
	}

	public void addDataCurrent(DataSource current2) {
		current = current2;
		sourceList.add(current2);
	}
}
